*===============================================================================
* Title: Two Levels - Moderator Variables
* Author: Agatha, Alex
*===============================================================================


*** interest in politics
foreach i of numlist 1/8 0 {
	gen interest`i'=(kp`i'_010-5)/-4
	lab var interest`i' "interest in politics"
	gen intkat`i'=0 if kp`i'_010==4 | kp`i'_010==5
	replace intkat`i'=1 if kp`i'_010==3
	replace intkat`i'=2 if kp`i'_010==1 | kp`i'_010==2
}


* interest in first wave (before cmapaign has started)
gen sinterest=interest1
replace sinterest=interest0 if gruppe==3
replace sinterest=interest2 if gruppe!=3 & missing(sinterest)
replace sinterest=interest3 if gruppe!=3 & missing(sinterest)
fre sinterest

* interest in politics by level
fre kp9_011*
gen interest_B=(kp9_011a-5)/-4 
gen interest_L=(kp9_011b-5)/-4 
gen interest_E=(kp9_011c-5)/-4
lab var interest_B "interest in politics, national level"
lab var interest_L "interest in politics, regional level"
lab var interest_E "interest in politics, european level"

* interest in the election, federal
foreach wave of numlist $allpre {
	gen interest_el_B`wave'=(kp`wave'_390-5)/-4 if !missing(kp`wave'_390)
	lab var interest_el_B`wave' "Interest in federal election campaign"
	}

* interest in the election, provincial
foreach wave of numlist $btwel {
	gen interest_el_L`wave'=(kp`wave'_393-5)/-4 if !missing(kp`wave'_393)
	lab var interest_el_L`wave' "Interest in provincial election campaign"
	}
	

* regional identity

fre kp3_2200b
gen regid1=(kp3_2200b-1)/4
gen regid2=(kp3_2200b-kp3_2200c)/4
gen regidkat=0 if kp3_2200b==1 | kp3_2200b==2
replace regidkat=1 if kp3_2200b==3
replace regidkat=2 if kp3_2200b==4 | kp3_2200b==5
gen regidkat2=1 if !missing(kp3_2200b,kp3_2200c)
replace regidkat2=0 if !missing(kp3_2200b,kp3_2200c) & kp3_2200b<kp3_2200c
replace regidkat2=2 if !missing(kp3_2200b,kp3_2200c) & kp3_2200b>kp3_2200c
fre regid?

						
*-----------------------------------
* Knowledge
*----------------------------------
* count of unknown candidates
* AfD candidate regional level only in wave 9
foreach x of numlist $all {
	gen B_dkcand`x'=0 if !missing(kp`x'_650a)
	replace B_dkcand`x' = 1 if kp`x'_650a == .p
	replace B_dkcand`x'= B_dkcand`x'+1 if kp`x'_650z == .p
	replace B_dkcand`x'= B_dkcand`x'+1 if kp`x'_650s == .p
	replace B_dkcand`x'= B_dkcand`x'+1 if kp`x'_650w == .p
	replace B_dkcand`x'= B_dkcand`x'+1 if kp`x'_650y == .p
	replace B_dkcand`x'= B_dkcand`x'+1 if kp`x'_650x == .p
	
	replace B_dkcand`x' =	B_dkcand`x'/6 if !missing(B_dkcand`x')
	lab var B_dkcand`x' "count of unknown candidates, national"
	
}

foreach x of numlist 5/9 {
	gen L_dkcand`x'=0 if !missing(kp`x'_650e1)
	replace L_dkcand`x' = 1 if kp`x'_650e1 == .p
	replace L_dkcand`x'= L_dkcand`x'+1 if kp`x'_650d1 == .p
	replace L_dkcand`x'= L_dkcand`x'+1 if kp`x'_650f1 == .p
	replace L_dkcand`x'= L_dkcand`x'+1 if kp`x'_650g1 == .p
	replace L_dkcand`x'= L_dkcand`x'+1 if kp`x'_650h1 == .p
	
	replace L_dkcand`x' =	L_dkcand`x'/5 if !missing(L_dkcand`x')
	lab var L_dkcand`x' "count of unknown candidates, regional"
}
*	replace L_dkcand9= L_dkcand9+1 if kp9_650k1 == .p
	

	
*** party identity
foreach i of numlist $all 0 {
gen strength`i'=(kp`i'_2100-1)/4
replace strength`i'=(strength`i'-1)*-1
}
	

foreach i of numlist $all 0 {
	gen unionpids`i'=0 if !missing(kp`i'_2090a)
	replace unionpids`i'=strength`i' if kp`i'_2090a==1
}
foreach i of numlist $all 0 {
	gen spdpids`i'=0 if !missing(kp`i'_2090a)
	replace spdpids`i'=strength`i' if kp`i'_2090a==4
}
foreach i of numlist $all 0 {
	gen fdppids`i'=0 if !missing(kp`i'_2090a)
	replace fdppids`i'=strength`i' if kp`i'_2090a==5
}
foreach i of numlist $all 0 {
	gen grupids`i'=0 if !missing(kp`i'_2090a)
	replace grupids`i'=strength`i' if kp`i'_2090a==6
}
foreach i of numlist $all 0 {
	gen linpids`i'=0 if !missing(kp`i'_2090a)
	replace linpids`i'=strength`i' if kp`i'_2090a==7
}
foreach i of numlist $all 0 {
	gen afdpids`i'=0 if !missing(kp`i'_2090a)
	replace afdpids`i'=strength`i' if kp`i'_2090a==322
}

drop strength*


*PID Imputed (use PID 8 or -if missing- any earlier observation)
foreach party in union spd fdp gru lin afd {
clonevar	`party'pids8_imp	=	`party'pids8
replace		`party'pids8_imp	=	`party'pids7 if missing(`party'pids8_imp)
replace		`party'pids8_imp	=	`party'pids6 if missing(`party'pids8_imp)
replace		`party'pids8_imp	=	`party'pids5 if missing(`party'pids8_imp)

}



* pid categorial

foreach i of numlist $all 0  {
recode kp`i'_2090a (801 808=0), gen(pid`i')
lab def pidcat ///
1 "CDU/CSU" ///
4 "SPD" ///
5 "FDP" ///
6 "Greens" ///
7 "The Left" ///
322 "AfD" ///
0 "no pid/other", modify
label val pid`i' pidcat
lab var pid`i' "PID"
}

*PID Imputed (use PID 17 or -if missing- any earlier observation)
clonevar pid8_imp	=	pid8
replace	pid8_imp	=	pid7 if missing(pid8_imp)
replace	pid8_imp	=	pid6 if missing(pid8_imp)
replace	pid8_imp	=	pid5 if missing(pid8_imp)

